package top.continew.admin.generator.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.common.controller.BaseController;
import java.util.Map;
import top.continew.admin.generator.model.query.TaskTodoQuery;
import top.continew.admin.generator.model.req.TaskTodoReq;
import top.continew.admin.generator.model.resp.TaskTodoDetailResp;
import top.continew.admin.generator.model.resp.TaskTodoResp;
import top.continew.admin.generator.service.TaskTodoService;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.List;

/**
 * 待办事项管理 API
 *
 * @author kellen
 * @since 2025/07/30
 */
@Tag(name = "待办事项管理 API")
@RestController
@CrudRequestMapping(value = "/generator/taskTodo", api = {Api.PAGE, Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE})
public class TaskTodoController extends BaseController<TaskTodoService, TaskTodoResp, TaskTodoDetailResp, TaskTodoQuery, TaskTodoReq> {

    @Operation(summary = "分页查询待办事项列表")
    @GetMapping("/page")
    public R<PageResp<TaskTodoResp>> pageTaskTodo(@RequestParam(defaultValue = "1") Integer page,
                                                  @RequestParam(defaultValue = "20") Integer size,
                                                  @RequestParam(required = false) String taskContent,
                                                  @RequestParam(required = false) Integer taskStatus,
                                                  @RequestParam(required = false) Long directorId) {
        return R.ok(baseService.pageTaskTodo(page, size, taskContent, taskStatus, directorId));
    }

    @Operation(summary = "获取用户列表")
    @GetMapping("/users")
    public R<List<Object>> getUserList() {
        return R.ok(baseService.getUserList());
    }

    @Operation(summary = "获取待办事项统计")
    @GetMapping("/stats/{userId}")
    public R<Map<String, Object>> getTaskTodoStats(@PathVariable Long userId) {
        return R.ok(baseService.getTaskTodoStats(userId));
    }

    @Operation(summary = "获取用户的待办事项列表")
    @GetMapping("/user/{userId}")
    public R<List<TaskTodoResp>> getUserTaskTodoList(@PathVariable Long userId, 
                                                     @RequestParam(required = false) Integer status) {
        return R.ok(baseService.getUserTaskTodoList(userId, status));
    }

    @Operation(summary = "更新待办事项状态")
    @PutMapping("/status/{id}")
    public R<Void> updateTaskStatus(@PathVariable Long id, 
                                    @RequestBody UpdateStatusReq req) {
        baseService.updateTaskStatus(id, req.getStatus(), req.getAttachments());
        return R.ok();
    }

    @Operation(summary = "处理待办事项（包含样品信息）")
    @PostMapping("/handleWithSample")
    public R<Void> handleTodoWithSample(@RequestBody HandleWithSampleReq req) {
        baseService.handleTodoWithSample(req);
        return R.ok();
    }

    @Operation(summary = "导出所有待办事项")
    @GetMapping("/exportAll")
    public void exportAllTodos(TaskTodoQuery query, jakarta.servlet.http.HttpServletResponse response) {
        baseService.exportAllTodos(query, response);
    }

    @Data
    @Schema(description = "更新状态请求")
    public static class UpdateStatusReq {
        @Schema(description = "状态", example = "1")
        private Integer status;
        
        @Schema(description = "附件列表")
        private List<TaskTodoReq.AttachmentInfo> attachments;
    }

    @Data
    @Schema(description = "处理待办事项请求")
    public static class HandleWithSampleReq {
        @Schema(description = "待办事项ID", example = "1")
        private Long todoId;
        
        @Schema(description = "处理状态", example = "2")
        private Integer status;
        
        @Schema(description = "是否需要样品", example = "1")
        private Integer needSample;
        
        @Schema(description = "邮寄地址")
        private String mailingAddress;
        
        @Schema(description = "附件列表")
        private List<TaskTodoReq.AttachmentInfo> attachments;
    }
}